package cn.schoolwow.workflow.test.module.oa.common.flow.step;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.test.module.oa.common.flow.common.ConvertWorkFlowDeployJumpCommonFlow;
import com.alibaba.fastjson.JSONObject;

public class LoopConvertChildNodeCommonFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        JSONObject topNode = flowContext.getData("topNode", JSONObject.class);

        JSONObject currentChildNode = topNode;
        while(null!=currentChildNode){
            flowContext.startFlow(new ConvertChildNodeCommonFlow())
                    .putCurrentCompositeFlowData("childNode", currentChildNode)
                    .execute();

            JSONObject nextChildNode = currentChildNode.getJSONObject("childNode");
            if(null==nextChildNode||nextChildNode.isEmpty()){
                break;
            }
            //添加当前节点和子节点的连接线
            flowContext.startFlow(new ConvertWorkFlowDeployJumpCommonFlow())
                    .putCurrentCompositeFlowData("currentChildNode", currentChildNode)
                    .putCurrentCompositeFlowData("nextChildNode", nextChildNode)
                    .execute();
            currentChildNode = nextChildNode;
        }
        flowContext.putTemporaryData("currentChildNode", currentChildNode);
    }

    @Override
    public String name() {
        return "循环处理并转换原始节点";
    }

}
